/* @(#)GSBalPayRecvManageQueryAction.java
 * 
 *  Project:GS
 *  
 *  Modify  Information:
 *  ===============================================
 *   Author          Date            Description
 *  -------------- ---------------  ---------------
 *  Jenny  Li      2006-5-16          新增
 * Copyright Notice:
 * =============================================================================
 *    Copyright (c) 2001-2006 Beijing HiTRUST Technology Co., Ltd. 1808 Room, 
 *        Science & Technology Building, No. 9 South Street, Zhong Guan Cun, 
 *        Haidian District, Beijing ,100081, China All rights reserved.
 *
 *    This software is the confidential and proprietary information of
 *        Beijing HiTRUST Technology Co., Ltd. ("Confidential Information").
 *        You shall not disclose such Confidential Information and shall use it
 *        only in accordance with the terms of the license agreement you entered
 *        into with HiTRUST.
 *
 * Warning:
 * =============================================================================
 *
 */
package com.hitrust.gs.action;

import java.util.ArrayList;
import java.util.HashMap;

import com.hitrust.gs.bean.GSOperatorBean;
import com.hitrust.gs.business.GSOrderDeptInfoProcessor;
import com.hitrust.gs.business.GSPayReceiveProcessor;
import com.hitrust.gs.dao.GSPayReceiveAbleDAO;
import com.hitrust.gs.form.GSBalPayRecvQryForm;
import com.hitrust.gs.lib.GSBaseConstants;
import com.hitrust.gs.service.GSBaseAction;
import com.hitrust.gs.service.GSBaseForm;
import com.hitrust.gs.service.GSBaseManager;
import com.hitrust.gs.util.date.DateUtil;
import com.hitrust.gs.util.page.ResultProcess;
import com.hitrust.gs.vo.GSBalanceManageQueryVO;

/**
 * @author JennyLi
 *
 */
public class GSBalPayRecvManageQueryAction extends GSBaseAction {

    public String execute(GSBaseForm aForm) throws Exception {
        //取得页面参数组装成查询VO对象
        GSBalanceManageQueryVO tVO = new GSBalanceManageQueryVO();
        GSBalPayRecvQryForm tForm = (GSBalPayRecvQryForm)aForm;
        tVO.orderDeptId = tForm.getOrderDeptId();
        tVO.provider = tForm.getProvider();
        tVO.deptCategory = tForm.getBalanceType();
        tVO.payType = tForm.getPayType();
        String[] tDateStr = new String[2];
        tDateStr[0] = tForm.getPayRecvStrDate();
        tDateStr[1] = tForm.getPayRecvEndDate();
        tDateStr = DateUtil.formateDate(tDateStr);
        tVO.payRecvStrDate = tDateStr[0];
        tVO.payRecvEndDate = tDateStr[1];
        tVO.status = tForm.getStatus();
        
        //取得当前操作员可查询的订货单位和送货地址
        GSOperatorBean tOperBean = (GSOperatorBean)aForm.getSession().getAttribute(GSBaseConstants.LOGIN);
        GSOrderDeptInfoProcessor tProc = new GSOrderDeptInfoProcessor();
        HashMap tMap = tProc.getOrderDeptInfoOfBusOper(tOperBean.getGs_oper_org_id());
        aForm.getRequest().setAttribute("orderdept",tMap);
        ArrayList tOrg = (ArrayList)tMap.get("organization");
        String[] tStr = this.getPageParams(tForm,tVO,tOrg);
        //查询应收应付信息
        GSPayReceiveProcessor tPayRecvProc = new GSPayReceiveProcessor();
        ArrayList tList = tPayRecvProc.getUnCompletedPayRecvInfo(tVO,Integer.parseInt(tStr[0]),Integer.parseInt(tStr[1]),tOrg);
        ResultProcess tRsProc = new ResultProcess();
        tRsProc.setResult(tList);
        tForm.getRequest().setAttribute("balpayreclist",tRsProc);
        //查询指定时间段内的应收应付（总计）信息
        //String tCurMonth = DateUtil.getCurrentDate("yyyyMM");
        ArrayList tPayRecList = tPayRecvProc.getCurMonthPayRecvInfo(tDateStr[0], tDateStr[1]);
        tForm.getRequest().setAttribute("curmonthpayrecvamt",tPayRecList);
        
        tForm.getRequest().setAttribute("pageSize",tStr[1]);
        tForm.getRequest().setAttribute("totalRecords",tStr[2]);
        return GSBaseConstants.FORWARD_SUCCESS;
    }
    private String[] getPageParams(GSBaseForm aForm,GSBalanceManageQueryVO aVO,ArrayList aOrg) throws Exception{
        String tStr[] = new String[3];
        int totalRecords;
        int pageNo;
        int pageSize;
        String tPage = GSBaseConstants.tPage;
        //根据页面传入的查询参数查询订单信息
        if(aForm.getChoice() == null || aForm.getChoice().equals("")){
            GSPayReceiveAbleDAO tDAO = new GSPayReceiveAbleDAO();
            totalRecords = tDAO.queryUnCompletedPayRecvInfoCount(aVO,aOrg);
            // 取得系统默认的页大小
            pageSize = Integer.parseInt(tPage);
            // PageNo=1
            pageNo = 1;
            int n = (totalRecords % pageSize == 0 )? totalRecords / pageSize:totalRecords / pageSize + 1;
            if (pageNo>n) {
                pageNo=n;
            }
            tStr[0] = "" + pageNo;
            tStr[1] = "" + pageSize;
            tStr[2] = "" + totalRecords;
        }else{
            tStr = GSBaseManager.getPageNoAndPageSize(aForm);
        }
        return tStr;
    }
}
